export default {
	apiWebHost: "https://bk.i23studio.com",
	adminToken: "tokenxm",
	ImgUrl: "https://bk.i23studio.com",
	token: "",
	APIURL: function(action) {
		return this.apiWebHost + action;
	},
	ImgFile: function(id, w, h) {
		if (id) {
			var url = this.ImgUrl + id;
			if (w) {
				url += "?resize=w_" + w;
			}
			if (h) {
				url += ",h_" + h;
			}
			return url;
		}
	},
	// 记录缓存
	setCookie: function(name, value) { //设置cookie，默认保存7天
		uni.setStorage({
			key: name,
			data: value,
			success: function() {
				console.log('success');
			}
		});
	},
	// 获取缓存
	getStorage: function(key, callback) {
		return new Promise((resolve) => {
			uni.getStorage({
				key: key,
				success: function(res) {
					resolve(res.data)
				},
				fail: function(res) {
					resolve(res.data)
				},
				complete: function(res) {
					resolve(res.data)
				},
			})
		})
	},
	// 上传
	upload(tempFilePaths) {
		return new Promise((resolve) => {
			let imgList = [];
			uni.showLoading({
				title:'正在上传中'
			})
			tempFilePaths.forEach((item, index) => {
				uni.uploadFile({
					url: 'https://bk.i23studio.com/api/common/upload',  //仅为示例，非真实的接口地址
					filePath: item,
					name: 'file',
					success: (uploadFileRes) => {
						let res = JSON.parse(uploadFileRes.data);
						console.log(res)
						if (res.code==1) {
							imgList.push(res.data.url); 
							if(index==(tempFilePaths.length-1)){
								uni.hideLoading()
								resolve(imgList)
							}
						}
					}
				});
			})

		})
	},
	// 上传文件
	ChooseMessage(num) {
		return new Promise((resolve) => {
			wx.chooseMessageFile({
				success: (res) => {
					const tempFilePaths = res.tempFiles;
					let uploadimg = this.upload(tempFilePaths)
					uploadimg.then((result) => {
						resolve(result)
					})
				}
			})
		});
	},	
	// 上传图片
	ChooseImage(num) {
		return new Promise((resolve) => {
			uni.chooseImage({
				count: num, //默认9
				sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
				sourceType: ['album', 'camera'], //从相册选择
				success: (res) => {
					const tempFilePaths = res.tempFilePaths;
					let uploadimg = this.upload(tempFilePaths)
					uploadimg.then((result) => {
						resolve(result)
					})
				}
			})
		});
	},
	// 上传视频
	ChooseVideo(num) {
		return new Promise((resolve) => {
			 uni.chooseVideo({
				count: 1, //默认9
				sourceType: ['camera', 'album'],
				success: (res) => {
					console.log(res)
					const tempFilePaths =[res.tempFilePath];
					let uploadimg = this.upload(tempFilePaths)
					uploadimg.then((result) => {
						resolve(result)
					})
				}
			})
		});
	},	
	//删除缓存
	remove: function(name) {
		return new Promise((resolve) => {
			uni.removeStorage({
				key: name,
				success: function(res) {
					resolve(res)
				}
			})
		})
	},
	ajax: function(opt) {
		opt = opt || {};
		opt.url = this.APIURL(opt.url) || '';
		opt.data = opt.data || null;
		opt.method = opt.method || 'GET';
		opt.header = opt.header || {
			'token': this.token || '',
			'content-type': 'application/json',
			'Accept': 'application/json',
		};
		opt.success = opt.success || function() {};
		uni.request({
			url: opt.url,
			data: opt.data,
			method: opt.method,
			header: opt.header,
			dataType: 'json',
			success: function(res) {
				let data = res.data
				opt.success(data);
			},
			fail: function(res) {},
			complete(res) {
				let data = res.data
				if (data.code!=1) {
					uni.showToast({
						title:data.msg,
						icon: 'none',
						duration: 1000
					});
				}
			}
		})
	},
	dateCalculation: function(data) { //返回天、时、分
		var newTime = date.replace(/\-/g, "/");
		let nowdata = new Date().getTime();
		let enddata = new Date(newTime).getTime();
		let second = parseInt(Math.abs(enddata - nowdata));
		if (second > 0) {
			let data = {};
			let day = parseInt(second / (1000 * 60 * 60 * 24));
			let hour = parseInt((second % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
			let minute = parseInt((second % (1000 * 60 * 60)) / (1000 * 60));
			let seconds = (second % (1000 * 60)) / 1000;
			data.day = day;
			data.hour = hour;
			data.minute = minute;
			data.seconds = seconds;
			return data;
		}
	},
	//格式化日期，format:格式，默认yyyy-MM-dd,date时间，默认当前时间
	getNowDate: function(format, date) {
		if (!format) {
			format = "yyyy-MM-dd";
		}
		var now = new Date();
		if (date) {
			// var newTime = date.replace(/\-/g, "/");
			now = new Date(date*1000);
		}

		var year = now.getFullYear(); //年
		var month = now.getMonth() + 1; //月
		var day = now.getDate(); //日
		var hh = now.getHours();
		var mm = now.getMinutes();
		var ss = now.getSeconds();

		var clock = year + "-";

		switch (format) {
			case "yyyy-MM-dd":
				{
					if (month < 10)
						clock += "0";
					clock += month + "-";
					if (day < 10) {
						clock += "0";
					}
					clock += day;
				}
				break;
			case "yyyy-MM":
				{
					if (month < 10)
						clock += "0";
					clock += month;
				}
				break;
			case "yyyy-MM-dd HH:mm":
				{
					if (month < 10)
						clock += "0";
					clock += month + "-";
					if (day < 10) {
						clock += "0";
					}
					clock += day + " ";

					if (hh < 10)
						clock += "0";
					clock += hh + ":";
					if (mm < 10)
						clock += "0";
					clock += mm + ":";
					if (ss < 10)
						clock += "0";
					clock += ss;
					clock = clock.substring(0, 16)
				}
				break;
			case "yyyy-MM-ddTHH:mm":
				{
					if (month < 10)
						clock += "0";
					clock += month + "-";
					if (day < 10) {
						clock += "0";
					}
					clock += day + "T";

					if (hh < 10)
						clock += "0";
					clock += hh + ":";
					if (mm < 10)
						clock += "0";
					clock += mm + ":";
					if (ss < 10)
						clock += "0";
					clock += ss;
					clock = clock.substring(0, 16)
				}
				break;
			case "yyyy-MM-dd HH:mm:ss":
				{
					if (month < 10)
						clock += "0";
					clock += month + "-";
					if (day < 10) {
						clock += "0";
					}
					clock += day + " ";

					if (hh < 10)
						clock += "0";
					clock += hh + ":";
					if (mm < 10)
						clock += "0";
					clock += mm + ":";
					if (ss < 10)
						clock += "0";
					clock += ss;
				}
				break;
			case "yyyyMMddHHmmss":
				{
					clock = year;
					if (month < 10)
						clock += "0";
					clock += month;
					if (day < 10) {
						clock += "0";
					}
					clock += day;

					if (hh < 10)
						clock += "0";
					clock += hh;
					if (mm < 10)
						clock += "0";
					clock += mm;
					if (ss < 10)
						clock += "0";
					clock += ss;
				}
				break;
			case "MMddHHmm":
				{
					clock = "";
					var nowdata = new Date();
					var nowYear = nowdata.getFullYear();
					var nowMonth = nowdata.getMonth() + 1;
					var nowDay = nowdata.getDate();
					var nowdatastr = nowYear + nowMonth + nowDay;
					var datastr = year + month + day;
					if (nowdatastr == datastr) {
						clock += hh + ":";
						clock += mm;
					} else {
						clock += month + "-";
						clock += day + " ";
						clock += hh + ":";
						clock += mm;
					}
				}
				break;
		}

		return clock;

	},
}
